﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Trebuchet
{

    public partial class PreferencesForm : Form
    {
        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
        private Preferences preferences;

        public Preferences Preferences
        {
            get { return preferences; }
            set { preferences = value; }
        }
        public PreferencesForm()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (preferences == null)
                preferences = new Preferences();
            int modifiers = 0;
            //None = 0,
            //Alt = 1,
            //Control = 2,
            //Shift = 4,
            //Windows = 8
            if (AltCheckBox.Checked == true)
                modifiers += 1;
            if (CtrlCheckBox.Checked)
                modifiers += 2;
            if (ShiftCheckBox.Checked)
                modifiers += 4;
            if (WindowsCheckBox.Checked)
                modifiers += 8;
            preferences.HotKeyModifiers = modifiers;
            preferences.HotKey = HotKeyComboBox.SelectedItem.ToString()[0];
            

            bool success = RegisterHotKey(this.Handle, this.GetType().GetHashCode(), preferences.HotKeyModifiers, (int)preferences.HotKey);
            if (!success)
            {
                MessageBox.Show("Unable to register Hot Key");
                DialogResult = DialogResult.None;
            }
            else
            {
                preferences.Save();
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void PreferencesForm_Load(object sender, EventArgs e)
        {
            if (preferences != null)
            {
                int remaining = preferences.HotKeyModifiers;

                while (remaining > 0)
                {
                    if (remaining >= 8)
                    {
                        remaining -= 8;
                        WindowsCheckBox.Checked = true;
                    }
                    if (remaining >= 4)
                    {
                        remaining -= 4;
                        ShiftCheckBox.Checked = true;
                    }
                    if (remaining >= 2)
                    {
                        remaining -= 2;
                        CtrlCheckBox.Checked = true;
                    }
                    if (remaining >= 1)
                    {
                        remaining -= 1;
                        AltCheckBox.Checked = true;
                    }
                }
                HotKeyComboBox.Text = preferences.HotKey.ToString();
            }
        }
    }
}
